package _01_动态数组;

public class _36_有效的数独 {

    public boolean isValidSudoku(char[][] board) {
        // 记录当前列是否存在当前值
        boolean[][] cols = new boolean[9][9];
        // 记录当前行存在什么值
        boolean[][] rows = new boolean[9][9];
        // 记录当前3宫格有当前值吗
        boolean[][] grads = new boolean[9][9];

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] != '.') {
                    int value = board[i][j] - '1';
                    int gradIndex = i / 3 * 3 + j / 3;
                    if (cols[i][value] || rows[j][value] || grads[gradIndex][value]) {
                        return false;
                    } else {
                        cols[i][value] = true;
                        rows[j][value] = true;
                        grads[gradIndex][value] = true;
                    }
                }
            }
        }
        return true;
    }

}
